<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">

            <!-- 放置表单 -->
            <el-form ref="form" :model="userForm" :rules="rules" label-width="120px" style="margin-left: 120px; margin-top:30px">
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="userForm.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码:" prop="password">
                <el-input v-model="userForm.password" style="width:300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="updateEmployeesInfo">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane :lazy="true" label="个人详情">
            <UserInfo />
          </el-tab-pane>
          <el-tab-pane :lazy="true" label="岗位信息">
            <JobInfo />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getEmployeesInfo, updateEmployeesInfo } from '@/api/employees.js'
import UserInfo from '../components/user-info.vue'
import JobInfo from '../components/job-info.vue'
export default {
  name: 'EmployeDetail',
  components: {
    UserInfo,
    JobInfo
  },
  data() {
    return {
      userForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, message: '密码长度最少6位', trigger: 'blur' }
        ]
      }
    }
  },

  created() {
    this.loadEmployeesInfo()
  },

  methods: {
    // 获取员工基本信息
    async loadEmployeesInfo() {
      const res = await getEmployeesInfo(this.$route.params.id)
      this.userForm = res
    },
    // 更新账户信息
    async updateEmployeesInfo() {
      try {
        await this.$refs.form.validate()
        await updateEmployeesInfo(this.userForm)
        // console.log('更新成功')
        this.$message.success('更新成功')
      } catch (err) {
        console.dir(err)
      }
    }
  }
}
</script>

<style scoped>
</style>
